/*
 * HttpRequest.h
 *
 *  Created on: 30/05/2014
 *      Author: vitor
 */

#ifndef HTTPREQUEST_H_
#define HTTPREQUEST_H_

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <memory>
#include <vector>
#include "TCPSocket.h"

class HttpRequest {
private:
	TCPSocket socket;
	std::string file;
	std::map<std::string, std::string> headers;
	std::vector<char> data;

public:
	enum RequestMethod {
		GET, POST, UNKNOWN
	};

	HttpRequest(TCPSocket socket);
	virtual ~HttpRequest();

	bool receive_request();
	void add_header(char* name, char* value);

	RequestMethod get_method() {
		return method;
	}

	std::string get_file() {
		return file;
	}

	std::string get_header(std::string header_name) {
		return headers[header_name];
	}

	std::vector<char>& get_data() {
		return data;
	}

	int get_data_size() {
		if (headers.count(std::string("Content-Length"))) {
			return atoi(headers[std::string("Content-Length")].c_str());
		}
		return 0;
	}

	void set_file(std::string file) {
		this->file = file;
	}

private:
	RequestMethod method = RequestMethod::UNKNOWN;

	void parse_method(char* method_name);
	char get_byte_from_stream();
	std::vector<char> get_line_from_stream();
	void get_request_line(char first_byte);
	void get_headers();
	void get_data_bytes();
};

#endif /* HTTPREQUEST_H_ */
